package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
   @Autowired
   private SetmealDishMapper setmealDishMapper;

   @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            for(int i = 0 ; i < flavors.size() ; i++) {
                flavors.get(i).setDishId(id);
            }}
            dishFlavorMapper.insertBatch(flavors);
        }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        log.info("菜品分页查询");
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        log.info("批量删除操作{}",ids);
        //观察菜品是否起手
        for(Long id : ids) {
            Dish dish =  dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE)
            {
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        List<Long> setmealId = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(!setmealId.isEmpty()) {
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL)
                    ;

        }
        //删除菜品以及关联的口味
//        for(Long id : ids) {
//            dishMapper.deleteDish(id);
//            dishFlavorMapper.deleteByDishId(id);
//        }
        dishMapper.deleteById(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public void status(String status, Integer id) {
        Integer statusDish = Integer.valueOf(status);
        dishMapper.uptateStatus(statusDish,id);
    }

    @Override
    public DishVO selectDishById(Long id) {
        log.info("根据Id查询操作{}",id);
        Dish dish = dishMapper.seleteDishById(id);
        List<DishFlavor> list = dishFlavorMapper.selectByIdF(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;

    }

    @Override
    public void update(DishDTO dishDTO) {
        log.info("修改菜品操作");
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
        }
    }

    @Override
    public List<Dish> selectList(Long categoryId) {
        log.info("操作");
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list = dishMapper.selectList(dish);
        return list;
    }
}
